//
//  MineWalletViewController.m
//  copyrightTrading
//
//  Created by 郭子一 on 2020/10/6.
//  Copyright © 2020 郭子一. All rights reserved.
//

#import "MineWalletViewController.h"
#import "MineWalletHeaderView.h"
#import "JXPagerView.h"
#import "TestListBaseView.h"
#import "MineWalletBandCardViewController.h"
#import "MineWalletRechagePopView.h"
#import "MineWalletWithdrawPopView.h"
#import "MineWalletBankCardManagerViewController.h"
static const CGFloat JXheightForHeaderInSection = 50;
@interface MineWalletViewController ()<JXPagerViewDelegate, JXCategoryViewDelegate, MineWalletRechagePopViewDelegate, MineWalletWithdrawPopViewDelegate>
@property (nonatomic, strong) JXPagerView *pagingView;
@property (nonatomic, strong) JXCategoryTitleView *categoryView;
@property (nonatomic, strong) NSArray <NSString *> *titles;
@property (nonatomic, strong) MineWalletHeaderView *walletHeaderView;

@property (nonatomic, strong) UIImageView *shadowImage;
@end

@implementation MineWalletViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGB_VALUE(0xDC0000);
}

- (void)setupUI
{
    UIView *headerView = [UIView new];
    headerView.backgroundColor = RGB_VALUE(0xDC0000);
    [self.view addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.height.mas_equalTo(Height_NavBar);
    }];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:[UIImage imageNamed:@"whiteBack"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setHitEdgeInsets:UIEdgeInsetsMake(-15, -15, -15, -15)];
    [headerView addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-14);
        make.left.offset(18);
        make.width.height.mas_equalTo(16);
    }];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"我的钱包";
    titleLabel.font = JSS_MEDIUM_FONT(17);
    titleLabel.textColor = RGB_VALUE(0xFFFFFF);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [headerView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-9);
        make.centerX.offset(0);
        make.width.mas_equalTo(135);
        make.height.mas_equalTo(24);
    }];
    
    _titles = @[@"支出", @"收入"];
    
    self.walletHeaderView = [[MineWalletHeaderView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, 263)];
//    [self.view addSubview:self.walletHeaderView];
    [self.walletHeaderView.rechargeBtn addTarget:self action:@selector(rechargeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.walletHeaderView.withdrawBtn addTarget:self action:@selector(withdrawBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.walletHeaderView.bankCardManagerBtn addTarget:self action:@selector(bankCardManagerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.walletHeaderView.bandCardBtn addTarget:self action:@selector(bandCardBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    _categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, JXheightForHeaderInSection)];
    self.categoryView.titles = self.titles;
    self.categoryView.backgroundColor = [UIColor whiteColor];
    self.categoryView.delegate = self;
    self.categoryView.titleSelectedColor = [UIColor blackColor];
    self.categoryView.titleColor = [UIColor blackColor];
    self.categoryView.titleColorGradientEnabled = YES;
    self.categoryView.titleLabelZoomEnabled = YES;
    self.categoryView.titleLabelZoomEnabled = YES;

    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    lineView.indicatorColor = RGB_VALUE(0x00B0FF);
    lineView.indicatorWidth = 60;
    lineView.indicatorHeight = 4;
    self.categoryView.indicators = @[lineView];

    _pagingView = [[JXPagerView alloc] initWithDelegate:self];
    [self.view addSubview:self.pagingView];

    self.categoryView.listContainer = (id<JXCategoryViewListContainer>)self.pagingView.listContainerView;
    
    [[APIManager sharedNetManager] requestWithMethod:HttpRequestTypePost URLString:@"useraccount" parameters:@{} done:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        self.walletHeaderView.balanceLabel.text = responseObject[@"data"][@"left_money"];
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    self.pagingView.frame = CGRectMake(0, Height_NavBar, kDeviceWidth, KDeviceHeight-Height_NavBar);
}

- (void)backBtnClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

// 充值
- (void)rechargeBtnClick:(UIButton *)sender
{
    MineWalletRechagePopView *view = [[MineWalletRechagePopView alloc] init];
    view.delegate = self;
}
// 提现
- (void)withdrawBtnClick:(UIButton *)sender
{
    MineWalletWithdrawPopView *view = [[MineWalletWithdrawPopView alloc] init];
    view.delegate = self;
}
// 银行卡管理
- (void)bankCardManagerBtnClick:(UIButton *)sender
{
    MineWalletBankCardManagerViewController *vc = [[MineWalletBankCardManagerViewController alloc] init];
    [self pushVc:vc];
}
// 绑定银行卡
- (void)bandCardBtnClick:(UIButton *)sender
{
    MineWalletBandCardViewController *vc = [[MineWalletBandCardViewController alloc] init];
    [self pushVc:vc];
}

#pragma mark - 充值弹窗点击确定按钮
- (void)RechagePopEnsureBtnClick
{
    NSDictionary *paramDic = @{
        @"money":@"10", // decimal类型的金额
        @"pay_type":@(1) // 充值方式 1微信 2银行卡
    };
    [[APIManager sharedNetManager] requestWithMethod:HttpRequestTypeGet URLString:@"recharge" parameters:paramDic done:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        

    }];
}

#pragma mark - 提现弹窗点击确定按钮
- (void)withdrawPopEnsureBtnClick
{
    NSDictionary *paramDic = @{
        @"money":@"10", // decimal类型的金额
        @"account":@"" // 提现的银行卡号
    };
    [[APIManager sharedNetManager] requestWithMethod:HttpRequestTypeGet URLString:@"encash" parameters:paramDic done:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        

    }];
}

#pragma mark - JXPagingViewDelegate

- (UIView *)tableHeaderViewInPagerView:(JXPagerView *)pagerView {
    return self.walletHeaderView;
}

- (NSUInteger)tableHeaderViewHeightInPagerView:(JXPagerView *)pagerView {
    return 263;
}

- (NSUInteger)heightForPinSectionHeaderInPagerView:(JXPagerView *)pagerView {
    return JXheightForHeaderInSection;
}

- (UIView *)viewForPinSectionHeaderInPagerView:(JXPagerView *)pagerView {
    return self.categoryView;
}

- (NSInteger)numberOfListsInPagerView:(JXPagerView *)pagerView {
    return self.titles.count;
}

- (id<JXPagerViewListViewDelegate>)pagerView:(JXPagerView *)pagerView initListAtIndex:(NSInteger)index {
    TestListBaseView *list = [[TestListBaseView alloc] init];
    if (index == 0) {
        list.walletType = 1;
    }else if (index == 1) {
        list.walletType = 2;
    }
    return list;
}

- (void)mainTableViewDidScroll:(UIScrollView *)scrollView {
//    [self.walletHeaderView scrollViewDidScroll:scrollView.contentOffset.y];
}
@end
